package com.woniuxy.usercoachassemble.controller;


import com.alibaba.fastjson.JSON;
import com.woniuxy.usercoachassemble.entity.Attention;
import com.woniuxy.usercoachassemble.entity.Inform;
import com.woniuxy.usercoachassemble.entity.Userinfo;
import com.woniuxy.usercoachassemble.service.AttentionService;
import com.woniuxy.usercoachassemble.service.InformService;
import com.woniuxy.usercoachassemble.service.UserService;
import com.woniuxy.usercoachassemble.service.UserinfoService;
import com.woniuxy.usercoachassemble.webservice.WebSocketServer;
import org.example.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-07-10
 */
@RestController
@RequestMapping("attention")
public class AttentionController {

    @Autowired
//查看所有关注的人
    private AttentionService attentionServiceImpl;


    @Autowired
    private InformService informService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private WebSocketServer webSocketServer;

    @RequestMapping("getattentionuser")
    public Result sellectattentionbyuserid(HttpServletRequest request) {
        int userId = request.getIntHeader("userId");
        return Result.Success(attentionServiceImpl.selectattentionbyuserid(userId));
    }

    //取消关注
    @RequestMapping("cancelattention")
    public void cancelattention(int f, int t) {
        attentionServiceImpl.cancellattenton(f, t);

    }

    @RequestMapping("addatention")
    public void addattention(Attention attention) {
        attentionServiceImpl.addattention(attention);

    }

    /**
     * @description:TODO(取关好友)
     * @param othersId 关注人id
     * @return:  org.example.Result
     * @author: Fy浪迹、
     * @date: 2020/7/21 23:18
     */
    @PostMapping("unfollow")
    public  Result unfollow(Integer othersId,HttpServletRequest request) throws IOException {
        // 自己id
        Integer userId = Integer.parseInt(request.getHeader("userId"));
        boolean remove = attentionServiceImpl.remove(attentionServiceImpl.query().eq("others_id", othersId).eq("oneself_id", userId).getWrapper());

        if(!remove){
            return Result.Error(600,"取关失败");
        }
        Result success = Result.Success();
        success.setMessage("取关成功!");

        Userinfo userinfo = userinfoService.query().eq("user_id", userId).one();
        // 发送关注消息
        Inform inform = new Inform();
        inform.setReadId(othersId);
        inform.setWriteId(userId);
        inform.setMessage("["+userinfo.getNickname()+"]" +"关注了你");
        inform.setType(1);
        inform.setStatus(1);
        inform.setTime(LocalDateTime.now());


        // websoct发送消息
        webSocketServer.sendInfo(JSON.toJSONString(inform),othersId + "");
        informService.save(inform);

        return success;
    }
    /**
     * @description:TODO(单字段查询用户)
     * @param field 用户名/昵称/电话
     * @param roleId 角色
     * @return:  org.example.Result
     * @author: Fy浪迹、
     * @date: 2020/7/21 23:47
     */
        @GetMapping("singleFieldSelectUser")
    public Result singleFieldSelectUser(String field,Integer roleId){
        List<Map<String, Object>> users = attentionServiceImpl.singleFieldSelectUser(field,roleId);
        return  Result.Success(users);
    }


    /**
     * @description:TODO(自己关注用户)
     * @param othersId 关注人用户id
     * @return:  org.example.Result
     * @author: Fy浪迹、
     * @date: 2020/7/22 10:35
     */
    @PostMapping("addAttentionByOthersId")
    public Result addAttentionByOthersId(HttpServletRequest request,Integer othersId) throws IOException {
        int userId = Integer.parseInt(request.getHeader("userId"));
        Attention one = attentionServiceImpl.query().eq("oneself_id", userId).eq("others_id", othersId).one();
        if(one != null){
            return  Result.Error(600,"你已关注此好友");
        }
        Attention attention = new Attention();
        attention.setOneselfId(userId);
        attention.setOthersId(othersId);
        attentionServiceImpl.save(attention);
        Result success = Result.Success();
        success.setMessage("关注成功");

        Userinfo userinfo = userinfoService.query().eq("user_id", userId).one();
        // 发送关注消息
        Inform inform = new Inform();
        inform.setReadId(othersId);
        inform.setWriteId(userId);
        inform.setMessage("["+userinfo.getNickname()+"]" +"关注了你");
        inform.setType(1);
        inform.setStatus(1);
        inform.setTime(LocalDateTime.now());


        // websoct发送消息
        webSocketServer.sendInfo(JSON.toJSONString(inform),othersId + "");
        informService.save(inform);

        return success;
    }






    // 查询与自己互相关注的人
    @GetMapping("getFriend")
    public Result getFriend(HttpServletRequest request) {
        int userId = Integer.parseInt(request.getHeader("userId"));
        System.out.println(userId);
        List<Map<String, Object>> maps = attentionServiceImpl.selectFriend(userId);
        return Result.Success(maps);
    }



}

